#!/usr/bin/python
# -*- coding: utf-8 -*-

import paho.mqtt.client as mqtt
import time
import logging
import threading
import subprocess
import json
import sys
results = []
p = subprocess.Popen("uci get sysinfo.@sysinfo[0].serial_num", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = []
#line = p.stdout.read()
#print line,
#for line in p.stdout.readlines() :
    #print line,
    #lines.append(line)
    #print lines
    #result["lines"] = lines
    #results.append(result)

def command(cmd, timeout=60): 
    """执行命令cmd，返回命令输出的内容。
    如果超时将会抛出TimeoutError异常。
    cmd - 要执行的命令
    timeout - 最长等待时间，单位：秒
    """ 
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) 
    t_beginning = time.time() 
    seconds_passed = 0 
    while True: 
        if p.poll() is not None: 
            break 
        seconds_passed = time.time() - t_beginning 
        if timeout and seconds_passed > timeout: 
            p.terminate() 
            raise TimeoutError(cmd, timeout) 
        time.sleep(0.1) 
    return p.stdout.read()

cmd=command("uci get sysinfo.@sysinfo[0].serial_num",timeout=3)
print cmd,
